/*
 * Machine.java
 *
 * Created on 19 de julio de 2007, 04:13
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ws;

import org.w3c.dom.Document;

/**
 *
 * @author mariano
 */
public class Machine extends xml.Xmlizable{
    
    private String name;
    private String status;
    
    public static final String STATUS_OFFLINE = "offline";
    public static final String STATUS_ONLINE = "online";
    
    /** Creates a new instance of Machine */
    public Machine() {
        this.status = Machine.STATUS_OFFLINE;
    }
    
    public Machine( String name, String status )
    {
        // por si el status que viene no es valido
        this.status = Machine.STATUS_OFFLINE;
        this.setStatus( status );
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        if( status != Machine.STATUS_OFFLINE && status != Machine.STATUS_ONLINE )
            return;
        
        this.status = status;
    }
    
    public xml.Coleccion toXmlColeccion()
    {
        xml.Coleccion col = new xml.Coleccion( "machine" );
        col.addNodo( "machine-name", name );
        col.addNodo( "machine-status", status );
        
        return col;
    }
    
    public static Machine fromXml( String str )
    {
        xml.Item col = xml.Item.fromXml( str );
        return parse( col );
    }
    
    public static Machine fromXml( Document doc )
    {
        xml.Item col = xml.Item.fromXml( doc.getFirstChild() );
        return parse( col );
    }
    
    private static Machine parse( xml.Item col )
    {
        if( !col.isColeccion() || col.getName() != "machine" )
            return null;
        
        xml.Coleccion c = (xml.Coleccion)col;
        
        xml.Nodo nName = c.getNodo( 0, "machine-name" );
        xml.Nodo nStatus = c.getNodo( 1, "machine-status" );
        
        if( nName == null || nStatus == null )
            return null;
        
        return new Machine( nName.getValue(), nStatus.getValue() );
    }
    
}
